package com.my.oschina.adapter;

import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.my.oschina.R;
import com.my.oschina.bean.FriendList.Friend;

public class ListViewPagerAdapter extends PagerAdapter {

	private OnRefreshListener<ListView> listener;
	private int pagerSize;
	private List<Friend> lvNewsDataList;

	public ListViewPagerAdapter(OnRefreshListener<ListView> listener) {
		this.listener = listener;
	}

	public ListViewPagerAdapter(OnRefreshListener<ListView> listener,
			int pagerSize) {
		this.listener = listener;
		this.pagerSize = pagerSize;
	}

	public ListViewPagerAdapter(OnRefreshListener<ListView> listener,
			int pagerSize, List<Friend> data) {
		this.listener = listener;
		this.pagerSize = pagerSize;
		this.lvNewsDataList = data;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {

		Context context = container.getContext();

		PullToRefreshListView plv = (PullToRefreshListView) LayoutInflater
				.from(context).inflate(R.layout.layout_listview_in_viewpager,
						container, false);

		// ListAdapter adapter = new ArrayAdapter<String>(context,
		// android.R.layout.simple_list_item_1, Arrays.asList(STRINGS));

		ListAdapter adapter2 = new ListViewFriendAdapter(context,
				lvNewsDataList, R.layout.friend_listitem);
		plv.setAdapter(adapter2);
		plv.setOnRefreshListener(listener);

		// Now just add ListView to ViewPager and return it
		container.addView(plv, LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);

		return plv;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public int getCount() {
		return pagerSize;
	}
}
